package com.avic.rong.adapter.collect;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.activity.ArticleDetailActivity;
import com.avic.rong.activity.VideoDetailActivity;
import com.avic.rong.activity.mine.CollectionActivity;
import com.avic.rong.api.collect.DeleteCollectApi;
import com.avic.rong.api.collect.ZanCancelApi;
import com.avic.rong.bean.collect.ArticleCollectBean;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.utils.ToastTools;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.mcxtzhang.swipemenulib.SwipeMenuLayout;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ArticleAdapter extends RecyclerView.Adapter {

    private Context mContext;

    private String token;

    private ArrayList<ArticleCollectBean> mList;
    private DeleteCollectApi deleteCollectApi;

    private ArrayList<Integer> pos;


    public ArticleAdapter(Context mContext, String token, ArrayList<ArticleCollectBean> mList) {
        this.mContext = mContext;
        this.token = token;
        this.mList = mList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_artical, viewGroup, false);
        return new ArticleHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        ArticleCollectBean bean = mList.get(i);
        ArticleHolder articleHolder = (ArticleHolder) viewHolder;

        int isV = bean.getIsV();
        if (isV == 1){
            articleHolder.ivVip.setVisibility(View.VISIBLE);
        }else {
            articleHolder.ivVip.setVisibility(View.GONE);
        }

        //用户头像
        Glide.with(mContext).applyDefaultRequestOptions(RequestOptions.circleCropTransform().error(R.mipmap.deafult_image_header)).load(bean.getLogo()).into(articleHolder.image_header);
        //名称
        articleHolder.tv_name.setText(bean.getUserName());
        //发布时间
        String publicTime = bean.getPublicTime();
        if (publicTime!=null) {
            String substring = publicTime.substring(0, 10).trim();
            articleHolder.tv_date.setText(substring);
        }
        //标题
        articleHolder.tv_title.setText(bean.getTitle());
        //商品图片
        if (bean.getGoodsImg()!=null){
            articleHolder.img_artical.setVisibility(View.VISIBLE);
            Glide.with(mContext).applyDefaultRequestOptions(RequestOptions.noTransformation()).load(bean.getGoodsImg()).into(articleHolder.img_artical);
        }else {
            articleHolder.img_artical.setVisibility(View.GONE);
        }
        //评论数
        articleHolder.tv_comment.setText(bean.getCommentsNum()+"");
        //收藏数
        articleHolder.tv_collect.setText(bean.getFavoritesNum()+"");
        //点赞数
        articleHolder.tv_zan.setText(bean.getLikeNum()+"");
        //访问数
        articleHolder.tv_toview.setText(bean.getViewNum()+"");

        articleHolder.cb_collect.setChecked(true);//默认是true

        articleHolder.tv_position.setText(bean.getAuthenInfo());//简介

        String isLike = bean.getIsLike();
        //是否点赞
        if (isLike.equals("1")){
            articleHolder.cb_zan.setChecked(true);
        }else if (isLike.equals("0")){
            articleHolder.cb_zan.setChecked(false);
        }


        //点赞
        articleHolder.cb_zan.setOnClickListener(new MyCheckedListener(articleHolder, bean,i));
        //收藏
        articleHolder.cb_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String trim = articleHolder.tv_collect.getText().toString().trim();
                Integer integer = Integer.valueOf(trim);

                boolean checked = articleHolder.cb_collect.isChecked();

                if (checked == false){
                    articleHolder.tv_collect.setText((integer-1)+"");
                }else if (checked == true){
                    articleHolder.tv_collect.setText((integer+1)+"");
                }


                deleteCollectApi = new DeleteCollectApi((CollectionActivity) mContext, new MyDeleteListener(bean,false,i));
                deleteCollectApi.setArgument(SharedPreferencesUtil.getLoginToken(), 12, bean.getTargetId());
                HttpManager.getInstance().doHttpDeal(deleteCollectApi);
            }
        });


        articleHolder.llArticle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (5 == bean.getTypeFlg()) {
                    Intent intent = new Intent(mContext, VideoDetailActivity.class);
                    intent.putExtra("contId", bean.getTargetId());
                    mContext.startActivity(intent);
                }else {
                    Intent intent = new Intent(mContext, ArticleDetailActivity.class);
                    intent.putExtra("contId", bean.getTargetId());
                    mContext.startActivity(intent);
                }
            }
        });

        articleHolder.collect_btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                articleHolder.finalView.quickClose();
                deleteCollectApi = new DeleteCollectApi((CollectionActivity) mContext, new MyDeleteListener(bean,true,i));
                deleteCollectApi.setArgument(SharedPreferencesUtil.getLoginToken(), 12, bean.getTargetId());
                HttpManager.getInstance().doHttpDeal(deleteCollectApi);
            }
        });

    }

    //收藏回调
    public class MyDeleteListener extends HttpOnNextListener<BaseResultEntity> {

        ArticleCollectBean bean;
        boolean isRefresh;
        int position;

        public MyDeleteListener(ArticleCollectBean bean, boolean isRefresh, int position) {
            this.bean = bean;
            this.isRefresh = isRefresh;
            this.position = position;
        }

        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            if (baseResultEntity.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                boolean data = (boolean) baseResultEntity.getData();
                if (isRefresh) {
                    mList.remove(bean);
                    notifyDataSetChanged();
                }
                ToastTools.showPublicToast(baseResultEntity.getMsg());
            }
        }
    };



    //点赞回调
    public class  MyHttpOnNextListener extends   HttpOnNextListener<BaseResultEntity> {

        ArticleHolder articleHolder;
        ArticleCollectBean bean;
        int position;
        public MyHttpOnNextListener(ArticleHolder articleHolder, ArticleCollectBean bean, int position) {
            this.position = position;
            this.bean = bean;
            this.articleHolder = articleHolder;
        }

        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            if (baseResultEntity.getCode().equals(ConstantsUtils.STATUS_SUCCESS_200)){
                String msg = baseResultEntity.getMsg();
                String trim = articleHolder.tv_zan.getText().toString().trim();
                Integer integer = Integer.valueOf(trim);
                if (msg.equals("点赞成功")){
                    bean.setIsLike("1");
                    integer = integer+1;
                }else {
                    bean.setIsLike("0");
                    integer = integer-1;
                }
                ToastTools.showPublicToast(msg);
                articleHolder.tv_zan.setText(integer+"");
            }else {
                ToastTools.showPublicToast(baseResultEntity.getMsg());
            }
        }
    };
    //点赞
    public class MyCheckedListener implements View.OnClickListener{

        ArticleHolder articleHolder;
        ArticleCollectBean bean;

        int position;

        public MyCheckedListener(ArticleHolder articleHolder, ArticleCollectBean bean, int i) {
            this.articleHolder = articleHolder;
            this.position = i;
            this.bean = bean;
        }
        @Override
        public void onClick(View v) {
            ZanCancelApi zanCancelApi = new ZanCancelApi((CollectionActivity) mContext,new MyHttpOnNextListener(articleHolder,bean,position));
            zanCancelApi.setArgument(bean.getTargetId(),1,token);
            HttpManager.getInstance().doHttpDeal(zanCancelApi);
        }
    }



    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ArticleHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_vip)
        ImageView ivVip;
        @BindView(R.id.finalView)
        SwipeMenuLayout finalView;
        @BindView(R.id.image_header)
        ImageView image_header;
        @BindView(R.id.tv_date)
        TextView tv_date;
        @BindView(R.id.img_artical)
        ImageView img_artical;
        @BindView(R.id.tv_position)
        TextView tv_position;
        @BindView(R.id.tv_title)
        TextView tv_title;
        @BindView(R.id.tv_toview)
        TextView tv_toview;
        @BindView(R.id.img_toview)
        CheckBox img_toview;
        @BindView(R.id.tv_collect)
        TextView tv_collect;
        @BindView(R.id.tv_zan)
        TextView tv_zan;
        @BindView(R.id.tv_comment)
        TextView tv_comment;
        @BindView(R.id.cb_collect)
        CheckBox cb_collect;
        @BindView(R.id.cb_zan)
        CheckBox cb_zan;
        @BindView(R.id.img_comment)
        CheckBox img_comment;
        @BindView(R.id.tv_name)
        TextView tv_name;
        @BindView(R.id.ll_artical)
        LinearLayout llArticle;
        @BindView(R.id.collect_btn_delete)
        Button collect_btn_delete;
        public ArticleHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

}
